package DBPool

import (
	"fmt"
)

/*
const (
	CONNECTIONS_NUM_PER_DATABASE = 16
	DATABASE_NUM                 = 2
	QUERY_CHAN_SIZE_PER_DATABASE = 2048
)
*/
var (
	DBPOOLCONFIG = NewDBPoolConfig()
)

type XmlDBInfo struct {
	Name     string `xml:"dbname,attr"`
	User     string `xml:"user,attr"`
	Password string `xml:"passwd,attr"`
	Address  string `xml:"addr,attr"`
	Port     string `xml:"port,attr"`
}

type XmlDBS struct {
	ConnSizePerDB uint32      `xml:"connsize,attr"`
	QueryChanSize uint32      `xml:"querychansize,attr"`
	DBInfo        []XmlDBInfo `xml:"instance"`
}

type XmlDBPoolConfig struct {
	DB XmlDBS `xml:"db"`
}

type DBAuthInfo struct {
	Name     string
	User     string
	Password string
	Address  string
	Port     string
}

type DBPoolConfig struct {
	Authors       []*DBAuthInfo
	ConnSizePerDB uint32
	QueryChanSize uint32
}

func NewDBPoolConfig() *DBPoolConfig {
	dbconfig := &DBPoolConfig{}
	return dbconfig
}

/*
 *  just make a simulation
 */
func (this *DBPoolConfig) LoadConfig(xmlconfig *XmlDBPoolConfig) {
	this.ConnSizePerDB = xmlconfig.DB.ConnSizePerDB

	this.QueryChanSize = xmlconfig.DB.QueryChanSize

	this.Authors = make([]*DBAuthInfo, len(xmlconfig.DB.DBInfo))

	for i, dbinfo := range xmlconfig.DB.DBInfo {
		this.Authors[i] = &DBAuthInfo{
			Name:     dbinfo.Name,
			User:     dbinfo.User,
			Password: dbinfo.Password,
			Address:  dbinfo.Address,
			Port:     dbinfo.Port,
		}
	}
}

func (this *DBPoolConfig) Dump() {
	fmt.Printf("-------------\n connsize: %d \n--------------------\n", this.ConnSizePerDB)
	fmt.Printf("-------------\n querychansize: %d \n--------------------\n", this.QueryChanSize)

	for i, dbinfo := range this.Authors {
		fmt.Printf("-------------\n dbnum:%d Name:%s User:%s Password:%s Address:%s Port:%s \n--------------------\n", i+1, dbinfo.Name, dbinfo.User, dbinfo.Password, dbinfo.Address, dbinfo.Port)
	}
}
